<%@ page pageEncoding="UTF-8"%>
<%@ include file="import.jsp"%>
<%!
	// 添加分组
	public Message createGroup(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
		doctorGroup.setDoctorUid(token.getUid());
		Integer id = createItem(doctorGroup);
		if (id == null) {
			setErrcode(msg, FAILURE);
			return msg;
		}
		msg.put("id", id);
		msg.put("group_name", doctorGroup.getGroupName());
		return msg;
	}

	// 修改分组
	public Message modifyGroup(Map<String, String> parameters) {
		Message msg = new Message();
		DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
		if (!modifyItem(doctorGroup)) {
			setErrcode(msg, NO_RESULT);
		}
		return msg;
	}

	// 查询分组
	public Message queryGroup(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
		doctorGroup.setDoctorUid(token.getUid());
		List list = QBuilder.db("basedb").select("new map(dg.id as group_id, dg.groupName as group_name, dg.content as content, dg.doctorUid as doctor_uid, count(gp.id) as num)").from("DoctorGroup as dg, GroupPatient as gp").where(new Where("dg.id = gp.groupId").and("dg.doctorUid = ",doctorGroup.getDoctorUid()).and("dg.status = ", STATUS_ENABLE).and("gp.status = ", STATUS_ENABLE).and("gp.patientUid is not null").and("dg.hospital = ", doctorGroup.getHospital()).and("gp.hospital = ", doctorGroup.getHospital())).group("gp.groupId").query().getResultObj();
		List list_total= QBuilder.db("basedb").queryHql("select distinct new map(id as group_id, groupName as group_name, doctorUid as doctor_uid) from DoctorGroup where doctorUid = " + doctorGroup.getDoctorUid() + " and status = " + STATUS_ENABLE + "and hospital = " + doctorGroup.getHospital());
		for (int i = 0; i < list_total.size(); i ++) {
			Map dg = (Map)list_total.get(i);
			int id = Tools.getInt(dg.get("group_id") + "");
			boolean isAdd = true;
			for (int j = 0; j < list.size(); j ++) {
				if (id == Tools.getInt(((Map)list.get(j)).get("group_id") + "")) {
					isAdd = false;
					break;
				}
			}
			if (isAdd) {
				dg.put("num", 0);
				list.add(dg);
			}
		}
		msg.put("doctor_groups",list);
		return msg;
	}

	// 删除分组
	public Message destroyGroup(Map<String, String> parameters) {
		Message msg = new Message();
		DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
		List<GroupPatient> list = QBuilder.db("basedb").queryHql("from GroupPatient where groupId = " + doctorGroup.getId() + " and status = " + STATUS_ENABLE + " and hospital = " + doctorGroup.getHospital());
		if (list.size() > 0) {
			setErrcode(msg, GROUP_DES_FAILED);
			return msg;
		}
		doctorGroup.setStatus(STATUS_DISABLE);
		if (!modifyItem(doctorGroup)) {
			setErrcode(msg, NO_RESULT);
			return msg;
		}
		return msg;
	}

	// 查询分组下的病人
	public Message queryPatient(Map<String, String> parameters) {
		Message msg = new Message();
		GroupPatient groupPatient = GroupPatient.fromMap(parameters, GroupPatient.class);
		List list = new ArrayList();
		Integer doctorUid = Tools.getInt(parameters.get("doctor_uid"), -1);
		if (groupPatient.getGroupId() == 0) { //所有用户
			list = QBuilder.db("basedb").select("distinct new map(dg.id as group_id, gp.patientUid as patient_uid, (case when gp.content is NULL then '' else gp.content end) as content, us.name as patient_name, us.age as age, us.gender as gender, us.medicalType as medical_type, us.phone as phone, us.cardNumber as card_number, u.avatar as avatar)").from("DoctorGroup as dg, GroupPatient as gp, UserProfile as us, User u").where(new Where("dg.id = gp.groupId").and("gp.patientUid = us.uid").and("dg.doctorUid = ", doctorUid).and("dg.status = ", STATUS_ENABLE).and("gp.status = ", STATUS_ENABLE).and("us.uid=u.id").and("us.status=", STATUS_ENABLE).and("u.status=", STATUS_ENABLE).and("us.self=0")).query().getResultObj();
		} else {   //具体分组
			list = QBuilder.db("basedb").select("new map(gp.groupId as group_id, gp.patientUid as patient_uid , gp.id as g_patient_id, (case when gp.content is NULL then '' else gp.content end) as content, u.name as patient_name, u.age as age, u.gender as gender, u.medicalType as medical_type, u.phone as phone, u.cardNumber as card_number, us.avatar as avatar)").from("GroupPatient as gp, UserProfile as u, User us").where(new Where("gp.patientUid = u.uid").and("gp.status = ", STATUS_ENABLE).and("u.status = ", STATUS_ENABLE).and("gp.hospital = ", groupPatient.getHospital()).and("u.hospital = ", groupPatient.getHospital()).and("gp.groupId = ", groupPatient.getGroupId()).and("u.uid=us.id").and("us.status=", STATUS_ENABLE).and("u.status=", STATUS_ENABLE).and("u.self=0")).query().getResultObj();
		}
		if(list.size() < 0) {
			setErrcode(msg, NO_RESULT);
		} else {
			msg.put("patients", list);
		}
		return msg;
	}

	// 删除分组下的病人
	public Message destroyPatient(Map<String, String> parameters) {
		Message msg = new Message();
		GroupPatient groupPatient = GroupPatient.fromMap(parameters, GroupPatient.class);
		groupPatient.setStatus(STATUS_DISABLE);
		if (!modifyItem(groupPatient)) {
			setErrcode(msg, NO_RESULT);
		}
		return msg;
	}

	// 添加病人的分组下
	public Message createPatient(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		String json = parameters.remove("groups");
		json = Tools.decryptStr(json);
		List<GroupPatient> list = GsonUtil.fromJson(json, new TypeToken<List<GroupPatient>>(){});
		List<GroupPatient> creates = new ArrayList<GroupPatient>();
		for (int i = 0; i < list.size(); i++) {
			GroupPatient groupPatient = list.get(i);
			parameters.put("patient_uid", groupPatient.getPatientUid() + "");
			List<GroupPatient> exits = QBuilder.db("basedb").queryHql("from GroupPatient where hospital = " + parameters.get("hospital") + " and status = " + STATUS_ENABLE + " and patientUid = " + groupPatient.getPatientUid() + " and groupId = " + groupPatient.getGroupId());
			if (groupPatient.getStatus() == 0) {
				if (exits.size() == 0) {
					continue;
				}
				GroupPatient source = exits.get(0);
				source.setStatus(STATUS_DISABLE);
				DBTools.db("basedb").modItem(source, source.getId());
			} else {
				groupPatient.setcTime(System.currentTimeMillis());
				groupPatient.setHospital(Tools.getInt(parameters.get("hospital"), -9999));
				if (groupPatient.getHospital() == -9999)
					continue;
				if (exits.size() >0)
					continue;
				creates.add(groupPatient);
			}
		}
		if (creates.size() > 0)
			DBTools.db("basedb").addItem(creates);
		return queryGroupPatient(parameters, token);
	}

	// 查询病人有的分组
	public Message queryGroupPatient(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
		doctorGroup.setDoctorUid(token.getUid());
		GroupPatient groupPatient = GroupPatient.fromMap(parameters, GroupPatient.class);
		List groups = QBuilder.db("basedb").queryHql("select new map(id as group_id, groupName as group_name) from DoctorGroup where hospital = " + doctorGroup.getHospital() + " and status = " + STATUS_ENABLE + " and doctorUid = " + doctorGroup.getDoctorUid());
		if (groups.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg;
		}
		List patients = QBuilder.db("basedb").queryHql("select new map(groupId as group_id, patientUid as patient_uid) from GroupPatient where hospital = " + groupPatient.getHospital() + " and status = " + STATUS_ENABLE + " and patientUid = " + groupPatient.getPatientUid());
		for (int i = 0; i < groups.size(); i++) {
			Map group = (Map)groups.get(i);
			String groupId = Tools.getDesc(group.get("group_id") + "", "-1223123");
			int status = 0;
			for (int j = 0; j < patients.size(); j++) {
				Map patient = (Map)patients.get(j);
				if (groupId.equals(Tools.getDesc(patient.get("group_id") + "", "-234234234"))) {
					status = 1;
					break;
				}
			}
			group.put("status", status);
		}
		msg.put("groups", groups);
		return msg;
	}

	// 通过姓名或手机号码查询
	public Message searchByNameOrPhone(Map<String, String> parameters){
		Message msg = new Message();
		List list = QBuilder.db("basedb").select("new map (us.name as patient_name, up.age as age, up.gender as gender, us.id as patient_uid, up.medicalType as medical_type, up.phone as phone, us.avatar as avatar, up.cardNumber as card_number)").from("User as us, UserProfile as up").where(new Where("us.id = up.uid").and("up.self=0").and("up.name like '%"+ parameters.get("keywords") +"%' or up.phone like '%" + parameters.get("keywords") + "%'").and("us.status = ", STATUS_ENABLE).and("up.status = ", STATUS_ENABLE).and("us.hospital = ",parameters.get("hospital")).and("up.hospital = ", parameters.get("hospital")).and("us.type = ", USER_PATIENT)).query().getResultObj();
		if (list.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg;
		}
		String patientUids = "-9";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			int patientUid = Tools.getInt(map.get("patient_uid") + "", -99);
			patientUids += ("," + patientUid);
		}
		List groups = QBuilder.db("basedb").queryHql("select new map(dg.groupName as group_name, dg.id as group_id, gp.patientUid as patient_uid, (case when gp.content is NULL then '' else gp.content end) as content) from DoctorGroup as dg, GroupPatient as gp where dg.id = gp.groupId and gp.status = " + STATUS_ENABLE + " and dg.status = " + STATUS_ENABLE + " and dg.hospital = " + parameters.get("hospital") + " and gp.hospital = " + parameters.get("hospital") + " and gp.patientUid in (" + patientUids + ") group by dg.id");
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			int patientUid = Tools.getInt(map.get("patient_uid") + "", -99);
			List groupNames = (List)map.get("group_names");
			if (groupNames == null)
				groupNames = new ArrayList();
			map.put("group_names", groupNames);
			for (int j = 0; j < groups.size(); j++) {
				Map group = (Map)groups.get(j);
				if (patientUid == Tools.getInt(group.get("patient_uid") + "", -999)) {
					groupNames.add(group);
				}
			}
		}
		msg.put("patients", list);
		return msg;
	}

	// 通过手机号码添加病人分组
	public Message createPatientByPhone(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		GroupPatient gp = GroupPatient.fromMap(parameters, GroupPatient.class);
		List list = QBuilder.db("basedb").select("new map (us.id as patient_uid)").from("User as us, UserProfile as up").where(new Where("us.id = up.uid").and("up.phone = ", parameters.get("phone")).and("us.status = ", STATUS_ENABLE).and("up.status = ", STATUS_ENABLE).and("us.hospital = ",parameters.get("hospital")).and("up.self=0").and("up.hospital = ", parameters.get("hospital")).and("us.type = ", USER_PATIENT)).group("up.age").query().getResultObj();
		if (list.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg;
		}
		Map map = (Map)list.get(0);
		int patientUid = Tools.getInt(map.get("patient_uid") + "", -9999);
		List<GroupPatient> exits = QBuilder.db("basedb").queryHql("from GroupPatient where hospital = " + parameters.get("hospital") + " and status = " + STATUS_ENABLE + " and patientUid = " + patientUid + " and groupId = " + gp.getGroupId());
		if (exits.size() > 0) {
			setErrcode(msg, USER_EXITS);
			return msg;
		}
		gp.setPatientUid(patientUid);
		DBTools.db("basedb").addItem(gp);
		return queryPatient(parameters);
	}

	// 通过Uid添加病人分组
	public Message createPatientByUid(Map<String, String> parameters, Token token) {
		Message msg = new Message();
		GroupPatient gp = GroupPatient.fromMap(parameters, GroupPatient.class);
		List<GroupPatient> exits = QBuilder.db("basedb").queryHql("from GroupPatient where hospital = " + parameters.get("hospital") + " and status = " + STATUS_ENABLE + " and patientUid = " + gp.getPatientUid() + " and groupId = " + gp.getGroupId());
		if (exits.size() > 0) {
			setErrcode(msg, USER_EXITS);
			return msg;
		}
		DBTools.db("basedb").addItem(gp);
		return queryPatient(parameters);
	}

	// 修改患者备注
	public Message remark(Map<String, String> parameters) {
		Message msg = new Message();
		List<GroupPatient> groupPatients = QBuilder.db("basedb").queryHql("from GroupPatient where hospital = " + parameters.get("hospital") + " and status = " + STATUS_ENABLE + " and patientUid = " + parameters.get("patient_uid"));
		if (groupPatients.size() == 0) {
			setErrcode(msg, FAILURE);
			return msg;
		}
		for (int i = 0; i < groupPatients.size(); i++) {
			GroupPatient gp = groupPatients.get(i);
			gp.setContent(parameters.get("content"));
		}
		DBTools.db("basedb").modItem(groupPatients);
		msg.put("content", Tools.getDesc(parameters.get("content"), ""));
		return msg;
	}

%>
